package com.ccsoft.broadcast;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

public class MainActivity extends Activity {
    private static final String TAG = "chanchaw";

    private IntentFilter intentFilter;
    private AToolsBroadcastReceiver rec;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 注册监听网络变化的广播接收器
        regBroadcastReceiverNetwork();
    }

    // 发送一条普通广播
    public void sendGeneral(View v){
        Intent intent = new Intent();
//        intent.putExtra("msg", "这是一条普通广播");
        intent.setAction("aGeneralBroadcast");
        sendBroadcast(intent);
        Log.i(TAG,"发送普通广播完毕！");
    }

    // 注册网络变化的广播接收器
    private boolean regBroadcastReceiverNetwork(){
        intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");// 网络状态变更广播
        intentFilter.addAction("android.net.conn.ACTION_SCREEN_OFF");// 熄屏广播
        intentFilter.addAction("android.net.conn.ACTION_SCREEN_ON");// 亮屏广播
        intentFilter.addAction("android.net.conn.ACTION_SHUTDOWN");// 关机广播
        intentFilter.addAction("android.net.conn.ACTION_USER_PRESENT");// 手机解锁广播
        rec = new AToolsBroadcastReceiver();
        registerReceiver(rec, intentFilter);
        return true;
    }
}
